// pages/register/register.js
var app = getApp();
var ea = require('../../utils/each.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    getQiNiuToken: app.data.url +'/zhongxue/maoapi/getqiniutoken',
    getstudent_info: app.data.url +'/zhongxue/maoapi/student_info',
    changestudent: app.data.url +'/zhongxue/maoapi/chenge_student',
    kong:true,
    haizhi:{},
    morentouxiang: app.data.url +'/uploads/zhongxue/jiajia.png',
    datas: {
      child_name: '向小英',   //孩子姓名
      sex: '男',         //性别
      birthday: '2016-09-10',    //生日
      relationship: '父女', //关系
      touxiang: app.data.url +"/uploads/zhongxue/xueshengtou.png",  //头像
      study: '成绩不好，尤其是数学，化学，文科成绩很好',  //学习状况
      bodys: '身体状况良好，很少生病，没有得过大病',   //身体状况
    },

    date: '2016-09-01',
    gister: app.data.url + '/zhongxue/maoapi/register',
    upload: app.data.url + '/zhongxue/maoapi/add_file',
    jiajia: app.data.url +"/uploads/zhongxue/jiajia.png",
    items: [
      { name: "男", chceked: true },
      { name: "女" }
    ],
    index: 0,
    array: ["母子", "母女", "父女", "父子", "其他"],
    arrays: ["6岁", "7岁", "8岁", "9岁", "10岁", "11岁", "12岁", "13岁", "14岁", "15岁", "16岁", "17岁", "18岁", "19岁", "20岁", "其他"],
  },

  //获取参数 
  bindinputs: function (e) {
    var value = e.detail.value;
    var key = e.currentTarget.dataset.key;
    var haizhi = this.data.haizhi;
    haizhi[key] = value;
    console.log(haizhi)
    this.setData({haizhi:haizhi});
    
  },
  // 获取性别
  radioChanges: function (e) {
    var es = e.detail.value;
    var haizhi = this.data.haizhi;
    haizhi.sex = es;
    console.log(haizhi);
    this.setData({
      haizhi: haizhi
    })
  },
  //获取生日
  bindDateChange: function (e) {
    // console.log(e);
    var value = e.detail.value;
    // console.log(value)
    var haizhi = this.data.haizhi;
    haizhi.birthday = value;
    console.log(haizhi)
    this.setData({ haizhi: haizhi})
    // let that = this;
    // var datas = that.data.datas;
    // datas["birthday"] = e.detail.value;
    // that.setData({
    //   datas: datas,
    //   date: e.detail.value
    // })
  },

  //获取亲属关系
  bindPickerChanges: function (e) {
    var value = e.detail.value;
    console.log(value);
    var shuju = this.data.haizhi.qingshu;
    var haizhi = this.data.haizhi;
    ea.each(shuju,function(i,v){
      if(i==value){
        haizhi.relationship = v.id;
      }
    })
    console.log(haizhi);
    this.setData({haizhi:haizhi,index:value});
  },


  //获取头像的照片
  upload: function () {
    var that = this;
    wx.chooseImage({
      count: 1, // 默认9
      sizeType: ['compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: res => {
         wx.showLoading({
           title: '上传中',
         })
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        var  tempFilePaths = res.tempFilePaths;
        ea.upfile(tempFilePaths,function(res){
          var src = res[0];
          var haizhi = that.data.haizhi;
          haizhi.touxiang = src;
          that.setData({haizhi:haizhi});
          wx.hideLoading();
        },that.data.getQiNiuToken)

      }
    })
  },




  //学习状况
  qis: function (e) {
    var vale = e.detail.value;
    // console.log(vale);
    var haizhi = this.data.haizhi;
    haizhi.study = vale;
    console.log(haizhi)
    this.setData({haizhi:haizhi})

  },
  //身体状况
  qiss: function (e) {
    var vale = e.detail.value;
    // console.log(vale);
    var haizhi = this.data.haizhi;
    haizhi.bodys = vale;
    console.log(haizhi)
    this.setData({ haizhi: haizhi })
  },



  //注册
  register: function (e) {
    var kong = this.data.kong;
    if(!kong){
      return;
    }
    this.setData({kong:false});
    var haizhi = this.data.haizhi;
    var user = wx.getStorageSync('user');
    haizhi.token = user.token;
    var that = this;
    wx.showLoading({
      title: '上传中',
    })
    wx.request({
      url: that.data.changestudent,
      data:haizhi,
      method:"POST",
      success:function(res){
        var data = res.data;
        if(data.code==200){
          wx.showToast({
            title: '保存成功',
            duration:app.data.quanbushijian
          })
          setTimeout(function(){
            wx.navigateBack({
              delta:1
            })
          },1300)
        }else{
          wx.showToast({
            title: data.msg,
            icon:'none',
            duration:app.data.quanbushijian
          })
        }
      }, complete:function(){
        wx.hideLoading()
        that.setData({kong:true});
      }
    })

  },



  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var id = options.id;
    console.log(id);
    // id = 5;
    var tiaojian ={};
    if(id){
      tiaojian.id = id;
    } 
      var user = wx.getStorageSync('user');
      tiaojian.token = user.token;
      var that = this;
      wx.showLoading({
        title: '加载中',
      })
      wx.request({
        url: that.data.getstudent_info,
        data: tiaojian,
        method: 'POST',
        success: function(res) {
          var data = res.data;
          console.log(data);
          if(data.code==200){
            var shuju = data.data;
            var tr = true;
            ea.each(shuju.qingshu,function(i,v){
              if (v.id == shuju.relationship){
                that.setData({index:i});
                tr = false;
              }
            })
            if(tr){
              shuju.relationship = shuju.qingshu[0].id;
              shuju.birthday = "2001-09-01";
              shuju.sex = '男';
            }
            console.log(shuju);
            that.setData({ haizhi: shuju});
          }else{
            wx.showToast({
              title: data.msg,
              icon:'none',
              duration:app.data.quanbushijian
            })
          }
          wx.hideLoading();
        },
        fail: function(res) {},
        complete: function(res) {},
      })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      path: '/pages/indexfirst/indexfirst',
    }
  }
})